//
//  CCAnimationter.swift
//  swift_weibo
//
//  Created by ccl on 16/3/5.
//  Copyright © 2016年 LittleBoy. All rights reserved.
//

import UIKit

class CCAnimationter: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    /// 是否是present的动画
    var ispresent = true;
    /// 显示的内容View的frame;
    var  presentViewFrame = CGRectZero;

    // MARK: UIViewControllerTransitioningDelegate 提供转场动画相关的对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let presentC = CCPresentationCtrl(presentedViewController: presented, presentingViewController: presenting);
        presentC.presentedViewFrame = presentViewFrame;
        return presentC;
    }
    // 返回 提供 modal动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        ispresent = true;
        return self;
    }
    
    // 返回 提供dimiss动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        ispresent = false;
        return self;
    }
    
    // MARK: UIViewControllerAnimatedTransitioning 提供转场动画的实现
    // 返回动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return ispresent ? 1.2 : 0.25;
    }
    
    // 自定义转场动画
    func  animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        //取出跳转前后的控制器
        // 原来控制器 转场到 下一控制器
        //     let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        // 下一控制器 返回到 原来控制器.
        //    let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        
        
        
        // 取出 上面设置的动画时间
        let animateDuration = transitionDuration(transitionContext);
        
        if ispresent{
            // 转场出现,动画
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            // 添加到转场动画上下文
            transitionContext.containerView()?.addSubview(toView);
            //设置动画
            toView.transform = CGAffineTransformMakeScale(0, 0);
            toView.layer.anchorPoint = CGPointMake(0.5, 0);
            
            
            UIView.animateWithDuration(animateDuration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 8, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                // 还原view原始大小
                toView.transform = CGAffineTransformIdentity;
                
                
                }, completion: { (_) -> Void in
                    // 动画结束后 告诉系统 转场结束
                    transitionContext.completeTransition(true);
                    
            })
            
            
        }else{
            // 转场消失动画
            let backView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            UIView.animateWithDuration(animateDuration, animations: { () -> Void in
              // 写成0,不好使, 要写成很小的数,才有效果.
                backView.transform = CGAffineTransformMakeScale(1.0, 0.0001);
                }, completion: { (_) -> Void in
                 backView.removeFromSuperview()
                    transitionContext.completeTransition(true);

            })
        }
        
    }

    
    
}
